#include <sys/mman.h>  
#include <sys/stat.h>  
#include <fcntl.h>  
#include <stdio.h>  
#include <stdlib.h>  
#include <memory.h>  
#include <unistd.h>  
#include <error.h> 
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <memory.h>  
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "drvsapi_common.h"
#include "libsapi_common.h"
#include "drvsapi_video.h"
#include "libsapi_video.h"

#include "vcap_thread.h"




extern pthread_mutex_t vcap_mutex;
extern pthread_cond_t  vcap_start_ready;
extern pthread_t       vcap_thread;

int vcap_process()
{
	/*wakeup net thread to return process result*/
	pthread_cond_signal(&vcap_start_ready);

	return 0;
}

void *video_cap_thread()
{
	int fd;
	struct stat sb;
	char *mapped;
	int cmd=0;
	int i=0;
	int ret=0;

	printf("i am vcap function\n");

	fd = open("/dev/comdrvs/fb", O_RDWR);
	if (fd < 0) {
		printf("open file error\n");
		ret = -1;
		goto err_out;
	}

	if ((fstat(fd, &sb)) == -1) {
		printf("get fstat error\n");
		ret = -1;
		goto err_out;
	}

	mapped = (char *)mmap(NULL, FRAMEBUFFER_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (mapped == (void *)-1) {
		printf("mmap error\n");
		ret = -1;
		goto err_out;
	}

	while(1) {
		pthread_cond_wait(&vcap_start_ready, &vcap_mutex);
		cmd=3;

		switch (cmd) {

		case 3: {//COMDRVS_CAP_INIT
			struct comdrvs_mmap_params params;
			
			params.baseaddr         = FRAME_BUFFER_OFFSET_PHY_DDR;
			params.offset_per_frame = READ_BUFFER_SIZE;
			params.width            = IMAGE_WIDTH;
			params.height           = IMAGE_HEIGHT;
			params.act_vid_in_en    = 1;
			params.rd_en            = 1;
			params.wr_en            = 1;
			ioctl(fd, COMDRVS_CAP_INIT, &params);
		}

		case 4:{
			unsigned int state = 0;

			ioctl(fd, COMDRVS_CAP_PREPARE_READ, &state);
			sleep(1);
		}

		case 5:{
			unsigned int state = 0;
			ioctl(fd, COMDRVS_CAP_GET_READ_STATE, &state);
		}

		case 6: {//COMDRVS_CAP_INIT
			int wrfd = 0;
			int count = 0;

			wrfd = open(RAW_FILE_NAME, O_CREAT | O_RDWR);
			if (fd < 0) {
				printf("open file error\n");
				ret = -1;
				goto err_out;
			}

			count = write(wrfd, mapped, READ_BUFFER_SIZE);
			if (count < READ_BUFFER_SIZE) {
				printf("write less data\n");
				ret = -1;
				goto err_out;
			}

			close(wrfd);
		}

		case 2: {//COMDRVS_CAP_INIT
			int rdfd = 0,rdfd8_h = 0,rdfd8_l = 0;
			int count = 0;
			char *rawbuf = malloc(1280*720*2);
			char *raw8buf_h = malloc(1280*720);
			char *raw8buf_l = malloc(1280*720);

			if(rawbuf==NULL || raw8buf_h==NULL || raw8buf_l==NULL){
				printf("can not mallloc buffer\n");
				ret = -1;
				goto err_out;
			}

			memset(rawbuf, 0, READ_BUFFER_SIZE);
			memset(raw8buf_h, 0, READ_BUFFER_SIZE/2);
			memset(raw8buf_l, 0, READ_BUFFER_SIZE/2);

			rdfd = open(RAW_FILE_NAME, O_RDONLY);
			if (rdfd < 0) {
				printf("open file error\n");
				ret = -1;
				goto err_out;
			}
			count = read(rdfd, rawbuf, WRITE_BUFFER_SIZE);
			if (count < WRITE_BUFFER_SIZE) {
				printf("read less data\n");
				ret = -1;
				goto err_out;
			}

			for(i=0; i<READ_BUFFER_SIZE/2; i++){
				raw8buf_h[i] = rawbuf[2*i+1];
				raw8buf_l[i] = rawbuf[2*i];
			}

			rdfd8_l = open(RAW_L_FILE_NAME, O_CREAT | O_RDWR);
			if (rdfd < 0) {
				printf("open file error\n");
				ret = -1;
				goto err_out;
			}
			count = write(rdfd8_l, raw8buf_l, READ_BUFFER_SIZE/2);
			if (count < READ_BUFFER_SIZE/2) {
				printf("write less data\n");
				ret = -1;
				goto err_out;
			}

			rdfd8_h = open(RAW_H_FILE_NAME, O_CREAT | O_RDWR);
			if (rdfd < 0) {
				printf("open file error\n");
				ret = -1;
				goto err_out;
			}
			count = write(rdfd8_h, raw8buf_h, READ_BUFFER_SIZE/2);
			if (count < READ_BUFFER_SIZE/2) {
				printf("write less data\n");
				ret = -1;
				goto err_out;
			}

			close(rdfd);
			close(rdfd8_l);
			close(rdfd8_h);
			free(rawbuf);
			free(raw8buf_h);
			free(raw8buf_l);
		}

			printf("cap one frame\n");
		default:
			break;
		}

		if(cmd==10){
			break;
		}

	}


	return (void*)ret;


err_out:
	return (void*)-1;

}
